<?php

namespace app\api\controller\company;
use app\shop\service\company\Staff as StaffService;
use app\api\service\company\Staff as StaffService2;
use app\shop\model\company\Staff as StaffModel;
/**
 * 员工管理
 */
class Staff
{
    /**
     * 员工列表
     */
    public function list(StaffService $StaffService)
    {
        $data = input();
        $my_staff = $data['my_staff'] ?? false;
        if ($my_staff) {
            $staff = StaffModel::with(['department'])->where('user_id', request()->user_id)->find();
            if (!$staff['department']['is_hr']) {
                $data['department_id'] = $staff['department_id'];
            }
        }
        return $StaffService->lists($data);
    }

    //我的信息
    public function me(StaffService $StaffService)
    {
        $user_id = request()->user_id;
        return $StaffService->detail($user_id);
    }

    public function detail(StaffService $StaffService)
    {
        $user_id = input('user_id');
        return $StaffService->detail($user_id);
    }

    public function files(StaffService $StaffService)
    {
        $user_id = input("user_id",request()->user_id);
        return $StaffService->files($user_id);
    }

    public function signEntry(StaffService $StaffService)
    {
        $data = input();
        $data['user_id'] = request()->user_id;
        return $StaffService->signEntry($data);
    }

    public function save(StaffService2 $StaffService)
    {
        $data = input();
        return $StaffService->editSelf($data);
    }

    public function leaveInfo(StaffService $StaffService)
    {
        $user_id = request()->user_id;
        return $StaffService->leaveInfo($user_id);
    }

    public function signLeave(StaffService $StaffService)
    {
        $data = input();
        $data['user_id'] = request()->user_id;
        return $StaffService->signLeave($data);
    }
    
    public function addStaff(StaffService $StaffService){
        $data = input();
        $user_id = $data['user_id']??0;
        $staff = StaffModel::find($user_id);
        if($staff){
            return json(['code' => 400, 'msg' => "员工已存在"]);
        }
        $data['handle_staff'] = request()->user_id;
        return $StaffService->save($data);
    }
    
    public function addFile(StaffService $StaffService){
        $data = input();
        $user_id = $data['user_id']??0;
        $data['add_user'] = request()->user_id;
        return $StaffService->saveFile($data);
    }
}